package com.imay.live.view;

import android.content.Context;
import android.graphics.Paint;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.imay.live.R;

/**
 * Created by liangzhenxiong on 2017/04/07 0007.
 * 发现-热门-文本自适应，图标居中
 */

public class HorMeasureLinearLayout extends ViewGroup {
    int mhot_live_nameleft;
    int mpaintheight;
    int mpadding;

    public HorMeasureLinearLayout(Context context) {
        super(context);
        init(context);
    }

    public HorMeasureLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public HorMeasureLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public HorMeasureLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {//只对这两个控件做布局
        int w = getMeasuredWidth();
        View tv_hotname_id = findViewById(R.id.tv_hotname_id);
        View tv_hotstatus_id = findViewById(R.id.tv_hotstatus_id);
        int ch = tv_hotname_id.getMeasuredHeight();
        int cw = tv_hotname_id.getMeasuredWidth();
        int top = (getMeasuredHeight() - tv_hotstatus_id.getMeasuredHeight()) >> 1;
        if ((w - cw - mhot_live_nameleft - mpadding - ch) > 0) {
            tv_hotname_id.layout(mhot_live_nameleft, 0, mhot_live_nameleft + cw, ch);
            int ileft = mhot_live_nameleft + cw + mpadding;
            tv_hotstatus_id.layout(ileft, top, ileft + ch, top + tv_hotstatus_id.getMeasuredHeight());
        } else {
            tv_hotname_id.layout(mhot_live_nameleft, 0, w - ch - mpadding, ch);
            tv_hotstatus_id.layout(w - ch, top, w, top + tv_hotstatus_id.getMeasuredHeight());
        }
    }

    void init(Context context) {
        mhot_live_nameleft = (int) context.getResources().getDimension(R.dimen.hot_live_nameleft);
        mpadding = (int) context.getResources().getDimension(R.dimen.hot_live_imageleft);
        ImageView imageView = new ImageView(context);
        imageView.setId(R.id.tv_hotstatus_id);
        imageView.setImageResource(R.mipmap.ico_05_v_big);
        addView(imageView);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        Paint.FontMetrics fontMetrics = ((TextView) findViewById(R.id.tv_hotname_id)).getPaint().getFontMetrics();
        mpaintheight = (int) Math.ceil(fontMetrics.descent - fontMetrics.top) - 15;
        View tv_hotname_id = findViewById(R.id.tv_hotname_id);
        View tv_hotstatus_id = findViewById(R.id.tv_hotstatus_id);
        measureChild(tv_hotname_id, MeasureSpec.makeMeasureSpec(sizeWidth - mpaintheight - mpadding - mhot_live_nameleft, MeasureSpec.EXACTLY), heightMeasureSpec);
        measureChild(tv_hotstatus_id, MeasureSpec.makeMeasureSpec(mpaintheight, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(mpaintheight, MeasureSpec.EXACTLY));
        setMeasuredDimension(sizeWidth, tv_hotname_id.getMeasuredHeight());
    }
}
